#!/usr/bin/env python
# -*- coding: utf-8 -*-
from assertpy.assertions import Assertions
from assertpy.base import ExceptionConvertor

from app.core.exception_code import exceptionMessage


class BizException(Exception):
    def __init__(self, code, msg=''):
        self.code = code
        self.msg = exceptionMessage.get(code) % msg if msg else exceptionMessage.get(code)
        super(BizException, self).__init__(f'{self.code}:{self.msg}')


class StrExceptionConvertor(ExceptionConvertor):

    def get_exception(self, obj):
        return BizException(obj)


def initException():
    Assertions.add_exception_convertor(str, StrExceptionConvertor())
